package gruppe7.servlets;

import gruppe7.control.ServletController;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class RealmCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	/** sæt cookies og send videre til j_security_check - realm fix */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletController sc = ServletController.getInstance();
		String remPw = request.getParameter("remPw");
		String remUser = request.getParameter("remUser");
		
		// husk brugernavnet ?
		if (remUser != null) {
			sc.setUsernameCookie(response, request.getParameter("j_username"));
		} else {
			// slet den
			sc.deleteUsernameCookie(response);
		}	
		
		// husk passwordet?
		if (remPw != null) {
			sc.setPasswordCookie(response, request.getParameter("j_password"));
		} else {
			// slet den
			sc.deletePasswordCookie(response);
		}
		
		String forward = "j_security_check?j_username=" + request.getParameter("j_username")
        + "&j_password=" + request.getParameter("j_password");
		
		System.out.println(forward);
		
		response.sendRedirect(response.encodeRedirectURL(forward));
	}
	
}
